# -*- coding: utf-8 -*-
import json
import re

import requests
import execjs
from bs4 import BeautifulSoup

headers = {
    'Host': 'm.dianping.com',
    'Referer': 'https://m.dianping.com/suizhou?from=city_more',
    'User-Agent': 'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1'
}

html = requests.get("https://m.dianping.com/allcategory", headers=headers).text
print(html)
# 第一个参数为html文档字符串，第二个为解析器，有标准库解析器html.parser、lxml、xml和html5lib
soup = BeautifulSoup(html, "html.parser")
t = soup.find_all(text=re.compile("window.PAGE_INITIAL_STATE = "))[0].strip().split(" = ")[1].replace(";", "")
print("tttttttt",t)
data = json.loads(t)
print("data:",data)
r = "{"+re.findall(r'"_isInit":true},(.*?),"footer"', html)[0]+"}"
print("rrrrrrrrrrr",r)
print(json.loads(r))
# ctx = execjs.compile(
#     """
#     function toJson(jsonstr) {
#          JSON.parse(jsonstr);
#      }
#     """
# )
# print(ctx.call("toJson", t))

